//
//  BVHttpRequest.m
//  BuyVegetable
//
//  Created by yushanchang on 16/9/29.
//  Copyright © 2016年 com.yinding. All rights reserved.
//

#import "BVHttpRequest.h"

@implementation BVHttpRequest

+ (void)getWithUrlString:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30.0f;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
//    __weak __typeof(self) weakSelf = self;
    [manager GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        if (success) {
            success(dict);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
//            [weakSelf requestFailed:error];
        } else {
//            [weakSelf requestFailed:error];
        }
    }];
}

+ (void)postWithUrlString:(NSString *)URLString parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *))failure{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 30.0f;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    __weak __typeof(self) weakSelf = self;
    [manager POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError *error = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];
        if (success) {
            success(dict);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
//            [weakSelf requestFailed:error];
        } else {
//            [weakSelf requestFailed:error];
        }
    }];
}

+ (void)uploadImageWithUrlString:(NSString *)URLString image:(UIImage *)image parameters:(id)parameters success:(void (^)(id))success failure:(void (^)(NSError *error))failure {
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer.timeoutInterval = 60.0f;
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];

    [manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        NSData *data = UIImagePNGRepresentation(image);
        NSString *str = [NSString stringWithFormat:@"%lf",[[NSDate date] timeIntervalSince1970]];
        NSString *fileName = [NSString stringWithFormat:@"%@.png",str];
        [formData appendPartWithFileData:data name:@"avatar" fileName:fileName mimeType:@"image/png"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSError *error = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:&error];

        if (success) {
            success(dict);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
    }];
}

// 请求失败回调方法
- (void)requestFailed:(NSError *)error{
    NSLog(@"error.code:\n%ld %@",(long)error.code, error.debugDescription);
    switch (error.code) {
        case AFNetworkErrorType_NoNetwork:
            NSLog(@"网络链接失败，请检查网络");
            break;
        case AFNetworkErrorType_TimedOut :
            NSLog(@"访问服务器超时，请检查网络");
            break;
        case AFNetworkErrorType_3840Failed :
            NSLog(@"服务器有错误，请稍后再访问");
            break;
        default:
            break;
    }
}


@end
